package com.yiyuwanglu.basecore.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiyuwanglu.basecore.common.Pager;
import com.yiyuwanglu.basecore.service.impl.WapDataServiceImpl;
import com.yiyuwanglu.basecore.util.TimesUtil;
import com.yiyuwanglu.basecore.vo.WapDataVo;
import com.yiyuwanglu.basecore.web.common.ResultMap;
import com.yiyuwanglu.basecore.web.common.ResultMap.STATUS;

@Controller("wapDataController")
@RequestMapping("/wapData")
public class WapDataController {

	@Autowired
	private WapDataServiceImpl wapDataService;

	@RequestMapping("/shopRecord")
	@ResponseBody
	public ResultMap shopRecord(WapDataVo wap, Pager page) {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId())) {
			List<WapDataVo> list = null;

			int count = wapDataService.shopRecordCount(wap);
			if (count > 0) {
				page.setTotalCount(count);
				list = wapDataService.shopRecord(wap, page);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("pager", page);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}

	@RequestMapping("/shopViewCount")
	@ResponseBody
	public ResultMap shopViewCount(WapDataVo wap) {
		ResultMap resultMap = new ResultMap();
		if (!StringUtils.isBlank(wap.getShopId())) {
			long shopViewCount = wapDataService.shopViewCount(wap);
			long shopProductViewCount = wapDataService.shopProductViewCount(wap);
			long shopRecordCount = wapDataService.shopRecordCount(wap);
			long shopShareCount = wapDataService.shopShareCount(wap);
			resultMap.setAttribute("shopViewCount", shopViewCount);
			resultMap.setAttribute("shopProductViewCount", shopProductViewCount);
			resultMap.setAttribute("shopRecordCount", shopRecordCount);
			resultMap.setAttribute("shopShareCount", shopShareCount);
			resultMap.setStatus(STATUS.SUCCESS);

		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}

	// 首页统计 访客数

	@RequestMapping("/shopViewCountAll")
	@ResponseBody
	public ResultMap shopViewCountAll(WapDataVo wap) {
		ResultMap resultMap = new ResultMap();
		if (!StringUtils.isBlank(wap.getShopId())) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (StringUtils.isBlank(wap.getMonthStart())) {
				wap.setMonthStart(sdf.format(TimesUtil.getTimesMonthmorning()));
			}
			if (StringUtils.isBlank(wap.getQueryTimes())) {
				wap.setQueryTimes(sdf.format(new Date()));
			}

			System.out.println(wap.getMonthStart());
			long shopDateView = wapDataService.shopViewCountAll(wap);
			long shopMonthView = wapDataService.shopViewSectionDate(wap);
			resultMap.setAttribute("shopDateView", shopDateView); // 当日访客数
			resultMap.setAttribute("shopMonthView", shopMonthView); // 日期访客数

			resultMap.setStatus(STATUS.SUCCESS);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}

	/***
	 *  查询浏览量 和访客数     
	 * @param wap
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/shopSeeCount")
	@ResponseBody
	public ResultMap shopSeeCount(WapDataVo wap) throws ParseException {
		ResultMap resultMap = new ResultMap();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (!StringUtils.isBlank(wap.getShopId())) {
			// 默认七天
			if (StringUtils.isBlank(wap.getMonthStart())) {
				wap.setMonthStart(sdf.format(TimesUtil.timsUpd(new Date(),-7)));
			}
			// 传过来已经转码
			// else{
			// wap.setMonthStart(sdf.format(TimesUtil.timsUpd(Integer.valueOf(wap.getMonthStart()))));
			// }
			// 浏览量
			long shopSeeCount = wapDataService.shopSeeCount(wap);
			List<WapDataVo> wl = wapDataService.shopSeeDateCount(wap);
			List m = new ArrayList();
			List m1 = new ArrayList();
			int allView = 0;
			for (int i = 0; i < wl.size(); i++) {
				HashMap map = new HashMap();
				map.put("date", wl.get(i).getCreateDate());
				map.put("count", wl.get(i).getCount());
				m.add(map);
				// 查询访客量
				wap.setMonthEnd(wl.get(i).getCreateDate());
				List<WapDataVo> wld = wapDataService.shopViewSectionList(wap);
				HashMap maps = new HashMap();
				// System.out.println(wld.size());
				// allView+=wld.size();
				maps.put("date", wl.get(i).getCreateDate());
				maps.put("count", wld.size());
				m1.add(maps);
			}
			// 访客量 需要根据每天统计 相加
			// long shopView = wapDataService.shopViewSectionDate(wap);

			// for (int i = 0; i < wld.size(); i++) {
			// HashMap map = new HashMap();
			// map.put("date", wld.get(i).getCreateDate());
			// map.put("count", wld.get(i).getCount());
			// map.put("wechatId", wld.get(i).getWechatId());
			// m1.add(map);
			// }
			allView = wapDataService.shopViewSectionDate(wap);
			resultMap.setAttribute("shopSeeCount", shopSeeCount);
			resultMap.setAttribute("shopSeeCountList", m);
			resultMap.setAttribute("shopView", allView); // 日期访客数
			resultMap.setAttribute("shopViewList", m1); // 日期 访客数 数组

			resultMap.setStatus(STATUS.SUCCESS);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}
		return resultMap;
	}
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 某店铺 以产品为维度    统计浏览量和访客数    20180425备注
	 * 
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/shopProductCount")
	@ResponseBody
	public ResultMap shopProductCount(WapDataVo wap, Pager page) throws ParseException {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId())) {
		
			// 默认七天
			if (StringUtils.isBlank(wap.getMonthStart())) {
				wap.setMonthStart(sdf.format(TimesUtil.timsUpd(new Date(),-7)));
			}

			// 访客量
			List<WapDataVo> ProductCountView = wapDataService.shopProductRecord(wap, page);

			// 浏览量
			List<WapDataVo> ProductCountSee = wapDataService.shopProductCountSee(wap, page);
			
			//分享量
			page.setPageNo(1);
			page.setPageSize(10000);
			wap.setCjmd("ALL");
			List<WapDataVo> shopProductShare = wapDataService.shopProductShare(wap, page);
			
			for (int i = 0; i < ProductCountSee.size(); i++) {
				for (int j = 0; j < ProductCountView.size(); j++) {
					if (ProductCountSee.get(i).getParam().equals(ProductCountView.get(j).getParam())) {
						ProductCountSee.get(i).setViewCount(ProductCountView.get(j).getCount());
					}
				}
				//分享量 默认设置为0
				ProductCountSee.get(i).setShareCount("0");
				for (int j = 0; j < shopProductShare.size(); j++) {
					if (ProductCountSee.get(i).getParam().equals(shopProductShare.get(j).getParam())) {
						ProductCountSee.get(i).setShareCount(shopProductShare.get(j).getCount());
					}
				}
			}
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("ProductCountView", ProductCountView);
			resultMap.setAttribute("ProductCountSee", ProductCountSee);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}
	
	/**
	 * 某店铺 以产品为维度    统计浏览量和访客数    20180425备注   以分享为主
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/shopShareProductCount")
	@ResponseBody
	public ResultMap shopShareProductCount(WapDataVo wap, Pager page) throws ParseException {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId())) {
		
			// 默认七天
//			if (StringUtils.isBlank(wap.getMonthStart())) {
//				wap.setMonthStart(sdf.format(TimesUtil.timsUpd(new Date(),-7)));
//			}
			//分享量
			wap.setCjmd("ALL");
			List<WapDataVo> shopProductShare = wapDataService.shopProductShare(wap, page);
			
			
			List share = new ArrayList(shopProductShare);
			
			// 访客量
			List<WapDataVo> ProductCountView = wapDataService.shopProductRecord(wap, page);
			// 浏览量
			List<WapDataVo> productCountSee = wapDataService.shopProductCountSee(wap, page);
			
			for (int i = 0; i < shopProductShare.size(); i++) {
				shopProductShare.get(i).setViewCount("0");
				shopProductShare.get(i).setShareCount(shopProductShare.get(i).getCount());
				shopProductShare.get(i).setCount("0");
				for (int j = 0; j < ProductCountView.size(); j++) {
					if (shopProductShare.get(i).getParam().equals(ProductCountView.get(j).getParam())) {
						shopProductShare.get(i).setViewCount(ProductCountView.get(j).getCount());
					}
				}
				//设置viewcount
				for (int j = 0; j < productCountSee.size(); j++) {
					if (shopProductShare.get(i).getParam().equals(productCountSee.get(j).getParam())) {
						String shareCount=shopProductShare.get(i).getShareCount();
						String viewCount=shopProductShare.get(i).getViewCount();
						productCountSee.get(j).setShareCount(shareCount);
						productCountSee.get(j).setViewCount(viewCount);
						share.set(i,productCountSee.get(j));
					}
				}
			}
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("ProductCountView", ProductCountView);
			resultMap.setAttribute("ProductCountSee", productCountSee);
			resultMap.setAttribute("productCountShare", share);
			
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}
	/***
	 * 某店铺下   产品的访问总次数    20180425备注
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shopProductRecord")
	@ResponseBody
	public ResultMap shopProductRecord(WapDataVo wap, Pager page) {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId())) {
			List<WapDataVo> list = null;
			int count = wapDataService.shopProductRecordCount(wap);
			if (count > 0) {
				page.setTotalCount(count);
				list = wapDataService.shopProductRecord(wap, page);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("pager", page);
			resultMap.setAttribute("list", list);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}
	/***
	 * 某店铺下 某客户 访问
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shopRecordDetail")
	@ResponseBody
	public ResultMap shopRecordDetail(WapDataVo wap, Pager page) {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId()) && !StringUtils.isBlank(wap.getWechatId())) {
			List<WapDataVo> list = null;
			int count = wapDataService.shopRecordDetailCount(wap);
			if (count > 0) {
				page.setTotalCount(count);
				list = wapDataService.shopRecordDetail(wap, page);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("pager", page);
			resultMap.setAttribute("list", list);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}

	/***
	 * 某店铺  查询单个产品的用户查看详细    20180425备注
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shopProductRecordDetail")
	@ResponseBody
	public ResultMap shopProductRecordDetail(WapDataVo wap, Pager page) {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId()) && !StringUtils.isBlank(wap.getParam())) {
			List<WapDataVo> list = null;
			int count = wapDataService.shopProductRecordDetailCount(wap);
			if (count > 0) {
				page.setTotalCount(count);
				list = wapDataService.shopProductRecordDetail(wap, page);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("pager", page);
			resultMap.setAttribute("list", list);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}

	/**
	 * 用户在某个店铺下面所有的访问记录
	 * 
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shopUserRecordDetail")
	@ResponseBody
	public ResultMap shopUserRecordDetail(WapDataVo wap, Pager page) {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId()) && !StringUtils.isBlank(wap.getWechatId())) {
			List<WapDataVo> list = null;
			int count = wapDataService.shopUserRecordDetailCount(wap);
			if (count > 0) {
				page.setTotalCount(count);
				list = wapDataService.shopUserRecordDetail(wap, page);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("pager", page);
			resultMap.setAttribute("list", list);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}
	
	
	/**
	 * 用户分享某个产品   次数
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shareShopProductCount")
	@ResponseBody
	public ResultMap shareShopProductCount(WapDataVo wap, Pager page) {
		ResultMap resultMap = new ResultMap();
		//产品code
		if ( !StringUtils.isBlank(wap.getShopId())&& !StringUtils.isBlank(wap.getParam())) {
			List<WapDataVo> list = null;
			int count = wapDataService.shopProductShareCount(wap);
			if (count > 0) {
				page.setTotalCount(count);
				list = wapDataService.shopProductShare(wap, page);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("pager", page);
			resultMap.setAttribute("list", list);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}
	
	/**
	 * 用户分享某个产品    被查看次数  
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shopProductSeeDetail")
	@ResponseBody
	public ResultMap shareShopProductDetail(WapDataVo wap, Pager page) {
		ResultMap resultMap = new ResultMap();

		if (!StringUtils.isBlank(wap.getShopId()) && !StringUtils.isBlank(wap.getParam())) {
			List<WapDataVo> list = null;
			int count = wapDataService.shopProductSeeDetailCount(wap);
			if (count > 0) {
				page.setTotalCount(count);
				list = wapDataService.shopProductSeeDetail(wap, page);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setAttribute("pager", page);
			resultMap.setAttribute("list", list);
		} else {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
		}

		return resultMap;
	}

	/**
	 * 店铺访问排行
	 * 
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shopVistSort")
	@ResponseBody
	public ResultMap shopVistSort(WapDataVo wap) {
		ResultMap resultMap = new ResultMap();

		List<WapDataVo> list = wapDataService.shopVistSort(wap);
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setAttribute("list", list);

		return resultMap;
	}

	/**
	 * 用户店铺访问名次
	 * 
	 * @param wap
	 * @param page
	 * @return
	 */
	@RequestMapping("/shopUserVistSort")
	@ResponseBody
	public ResultMap shopUserVistSort(WapDataVo wap) {
		ResultMap resultMap = new ResultMap();
		if (StringUtils.isBlank(wap.getShopId())) {
			resultMap.setAttribute("dto", null);
			resultMap.setStatus(STATUS.SUCCESS);
			return resultMap;
		}
		WapDataVo shopUserVistSort = wapDataService.shopUserVistSort(wap);

		resultMap.setAttribute("dto", shopUserVistSort);
		resultMap.setStatus(STATUS.SUCCESS);

		return resultMap;
	}
	
	
	/**
	 * 用户访问产品行为统计
	 */
	@RequestMapping("/userProject")
	@ResponseBody
	public ResultMap userProject(WapDataVo wap) {
		ResultMap resultMap = new ResultMap();
		String types =  "110207,110208,110205,110206,110507,110508,110509,110510,120201,120202,120203,120204";
		List<WapDataVo> list = wapDataService.userProject(wap,types);

		resultMap.setAttribute("list", list);
		resultMap.setStatus(STATUS.SUCCESS);
		return resultMap;
	}
	
	

}
